<template>
  <my-container v-loading="pageLoading">
    <!--查询-->
    <template #header>
      <el-form class="ad-form-query" :inline="true" @submit.native.prevent>
        <el-form-item v-if="checkPermission(['admin_sysmessage_add'])">
          <el-button type="primary" icon="el-icon-plus" @click="onAdd">新增消息</el-button>
        </el-form-item>
      </el-form>
    </template>

    <!--列表-->
    <el-table
      v-loading="listLoading"
      :data="msgList"
      border
      highlight-current-row
      :header-cell-style="{background:'#f9f9f9'}"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column type="index" width="80" label="#" />
      <el-table-column prop="type" label="消息类型" width>
        <template #default="{row}">
          <el-tag v-if="row.type === 0" type="success" size="small">通知</el-tag>
          <el-tag v-else-if="row.type === 1" type="primary" size="small">私信</el-tag>
          <el-tag v-else-if="row.type === 2" type="info" size="small">业务处理</el-tag>

        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" width />
      <el-table-column prop="toUserId" label="收件人" width />
      <el-table-column prop="creatorName" label="创建人" width />
      <el-table-column prop="createTime" label="创建时间" :formatter="formatCreatedTime" width />

      <el-table-column label="操作" width="180">
        <template #default="{ $index, row }">
          <my-confirm-button
            v-if="checkPermission(['admin_sysmessage_delete'])"
            type="delete"
            icon="el-icon-delete"
            :loading="row._loading"
            :validate-data="row"
            @click="onDelete($index, row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <my-pagination
      ref="pager"
      :total="total"
      :checked-count="sels.length"
      @get-page="getList"
    />
    <!--分页-->

    <!--新增窗口-->
    <el-dialog
      title="新增消息"
      :visible.sync="addFormVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <el-form ref="addForm" :model="addForm" label-width="80px" :rules="addFormRules" size="medium">

        <el-row>
          <el-form-item label="消息类型" prop="type">
            <el-select v-model="addForm.type" placeholder="请选择" style="width:100%;">
              <el-option label="通知" value="0" />
              <el-option label="私信" value="1" />
              <el-option label="业务处理" value="2" />
            </el-select>
          </el-form-item>

        </el-row>

        <el-row>
          <el-form-item label="收件人" prop="toUserId">

            <el-select v-model="addForm.toUserId" placeholder="请选择" multiple style="width:100%;">
              <el-option
                v-for="item in users"
                :key="item.id"
                :label="item.trueName"
                :value="item.id"
              />
            </el-select>

          </el-form-item>

        </el-row>

        <el-row>
          <el-form-item label="内容" prop="content">
            <el-input v-model="addForm.content" type="textarea" :rows="5" :cols="15" />
          </el-form-item>
        </el-row>

      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <my-confirm-button type="submit" :validate="addFormValidate" :loading="addLoading" @click="onAddSubmit" />
        </div>
      </template>

    </el-dialog>

  </my-container>
</template>

<script>
import { formatTime } from '@/utils'
import { getList, addMsg, removeMsg } from '@/api/admin/message'
import MyContainer from '@/components/my-container'
import MyConfirmButton from '@/components/my-confirm-button'
import { getUserListPage } from '@/api/admin/user'
export default {
  name: 'Roles',
  components: { MyContainer, MyConfirmButton },
  data() {
    return {
      msgList: [],
      users: [],
      total: 0,
      sels: [], // 列表选中列
      statusList: [
        { name: '激活', value: true },
        { name: '禁用', value: false }
      ],
      listLoading: false,
      pageLoading: false,
      addDialogFormVisible: false,
      addFormVisible: false, // 新增界面是否显示
      addLoading: false,
      addFormRules: {
        content: [{ required: true, message: '请输入消息内容', trigger: 'blur' }],
        type: [{ required: true, message: '请选择消息类型', trigger: 'change' }],
        toUserId: [{ required: true, message: '请选择选择收件人', trigger: 'change' }]
      },
      // 新增界面数据
      addForm: {
        content: ''
      },
      addFormRef: null,
      deleteLoading: false
    }
  },
  mounted() {
    this.getList()
  },
  beforeUpdate() {
  },
  methods: {
    formatCreatedTime: function(row, column, time) {
      return formatTime(time, 'YYYY-MM-DD HH:mm')
    },
    onSearch() {
      this.$refs.pager.setPage(1)
      this.getList()
    },
    // 获取角色列表
    async getList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager
      }
      this.listLoading = true
      const res = await getList(params)
      this.listLoading = false
      if (!res?.success) {
        return
      }

      this.total = res.data.totalCount
      const data = res.data.rows
      data.forEach(d => {
        d._loading = false
      })
      this.msgList = data
    },
    closeEditForm() {
      this.$refs.editForm.resetFields()
    },
    // 显示新增界面
    async onAdd() {
      this.addFormVisible = true
      const params = {
        pageSize: 1000,
        currentPage: 1

      }
      const res = await getUserListPage(params)
      this.users = res.data.rows
    },
    closeAddForm() {
      this.$refs.addForm.resetFields()
    },

    // 新增验证
    addFormValidate: function() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 新增
    async onAddSubmit() {
      this.addLoading = true
      const para = _.cloneDeep(this.addForm)

      const res = await addMsg(para)
      this.addLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.addOk'),
        type: 'success'
      })
      this.$refs['addForm'].resetFields()
      this.addFormVisible = false
      this.getList()
    },
    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = []
      para.push(row.id)
      const res = await removeMsg(para)
      row._loading = false

      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })

      this.getList()
    },

    selsChange: function(sels) {
      this.sels = sels
    }
  }
}
</script>
